package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.FamousTeacherDO;
import com.jinzhi.jzweb.pojo.FamousTeacherVO;
import com.jinzhi.jzweb.service.FamousTeacherService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/***
 * @author C
 * @Date 2023/12/6
 * @Description: TODO
 * @Version 1.0
 */
@Controller
@RequestMapping("/famousTeacher")
@Api(value = "名师管理", tags = "名师管理")
public class FamousTeacherController extends BaseController {

    @Autowired
    private FamousTeacherService famousTeacherService;

    /**
     * 条件分页
     *
     * @param pageNum
     * @param pageSize
     * @param famousTeacherVO
     * @return
     */
    @ResponseBody
    @GetMapping("/list/{pageNum}/{pageSize}")
    //@RequiresPermissions("jzweb:famousTeacher:list")
    @ApiOperation(value = "名师管理-分页查询", notes = "名师管理-分页查询")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result getAll(@PathVariable("pageNum") Integer pageNum,
                         @PathVariable("pageSize") Integer pageSize,
                         FamousTeacherVO famousTeacherVO) {
        return famousTeacherService.getPage(pageNum, pageSize, famousTeacherVO);
    }


    /**
     * 查询所有名师列表
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result getList(){
        List<FamousTeacherDO> list=famousTeacherService.getList();
        return Result.ok(list);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    @ApiOperation(value = "名师管理-根据id查询", notes = "名师管理-根据id查询")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result getById(@PathVariable("id") Long id) {
        FamousTeacherDO famousTeacherDO = famousTeacherService.getById(id);
        return Result.ok(famousTeacherDO);
    }

    /**
     * 新增
     *
     * @param famousTeacherVO
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @ApiOperation(value = "名师管理-新增", notes = "名师管理-新增")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @RequiresPermissions("jzweb:famousTeacher:add")
    public Result addFamousTeacher(@RequestBody FamousTeacherVO famousTeacherVO) {
        famousTeacherService.insertFamousTeacher(famousTeacherVO);
        return Result.ok("新增成功");
    }

    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("jzweb:famousTeacher:edit")
    @ApiOperation(value = "名师管理-修改", notes = "名师管理-修改")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result edit(@RequestBody FamousTeacherVO famousTeacherVO) {
        famousTeacherService.updateFamousTeacher(famousTeacherVO);
        return Result.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ResponseBody
    @RequiresPermissions("jzweb:famousTeacher:remove")
    @ApiOperation(value = "名师管理-删除", notes = "名师管理-删除")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result deleteById(@PathVariable("id") Long id) {
        famousTeacherService.deleteFamousTeacher(id);
        return Result.ok("删除成功");
    }


    @ApiOperation(value = "名师后台管理页面入口", notes = "名师后台管理页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/index")
    String getList(Model model) {
        List<Map> mylist = getRolePageButton("/famousTeacher/index");
        String authority = "";
        for (Map map : mylist) {
            authority += map.get("name") + ",";
        }
        authority = authority.length() > 0 ? authority.substring(0, authority.length() - 1) : "";
        model.addAttribute("authorityButtonAdd", authority.contains("新增"));
        model.addAttribute("authorityButtonEdit", authority.contains("修改"));
        model.addAttribute("authorityButtonInquire", authority.contains("查询"));
        model.addAttribute("authorityButtonDelete", authority.contains("删除"));
        model.addAttribute("authority", authority.split(","));
        return "jzweb/teacherManagement/index";
    }

    @ApiOperation(value = "名师后台管理新增页面入口", notes = "名师后台管理新增页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/add")
    String add(Model model) {
        String roleName = getRoleNames();
        String authority = "";
        if (roleName.contains("教师")) {
            authority = "教师";
        } else if (roleName.contains("管理员")) {
            authority = "管理员";
        } else {
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        return "jzweb/teacherManagement/add";
    }


    @ApiOperation(value = "名师后台管理修改页面入口", notes = "名师后台管理修改页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/update/{id}")
    String update(Model model, @PathVariable("id") Long id) {
        String roleName = getRoleNames();
        FamousTeacherDO famousTeacherDO = famousTeacherService.getById(id);
        String authority = "";
        if (roleName.contains("教师")) {
            authority = "教师";
        } else if (roleName.contains("管理员")) {
            authority = "管理员";
        } else {
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        model.addAttribute("userData", famousTeacherDO);
        return "jzweb/teacherManagement/edit";
    }

}
